package cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.supplier.controller.admin.supplier;

import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goodsType.TreeVo;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.supplier.controller.admin.supplier.vo.AreasDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.supplier.controller.admin.supplier.vo.SupplierPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.supplier.controller.admin.supplier.vo.WeekTimes;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.supplier.dal.dataobject.supplier.SupplierDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.supplier.dal.mysql.supplier.SupplierMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.supplier.mapper.AreasMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.supplier.service.supplier.SupplierService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 小程序-供应商")
@RestController
@RequestMapping("/qd/supplier")
@Validated
public class SupplierController {

    @Resource
    private SupplierService supplierService;
    @Resource
    AreasMapper areasMapper;
    @Resource
    SupplierMapper supplierMapper;

    //获取供应商下拉选
    @GetMapping("getSupList")
    public CommonResult getSupTree(){
        List<Map<String,Object>> list=new ArrayList<>();
        for (SupplierDO vo : supplierMapper.selectList()) {
            Map<String,Object> map=new HashMap<>();
            map.put("supplierId",vo.getId());
            map.put("supplierName",vo.getName());
            list.add(map);
        }
        return success(list);
    }

    //获取地址树
    @GetMapping("getTree")
    public CommonResult getTree(){
        List<TreeVo> trees=new ArrayList<>();
        List<AreasDO> areasDOS = areasMapper.selectList(new LambdaQueryWrapper<AreasDO>());

        for (AreasDO vo : areasDOS) {
            if (vo.getParentId()==100000)
                trees.add(new TreeVo(vo.getId(),vo.getName()));
        }
        trees=child(areasDOS, trees);
        for (TreeVo tree : trees) {
            tree.setChildren(child(areasDOS, tree.getChildren()));
        }

        return CommonResult.success(trees);
    }




    @PostMapping("/create")
    @Operation(summary = "创建小程序-供应商")
    @PreAuthorize("@ss.hasPermission('qd:supplier:create')")
    public CommonResult<Integer> createSupplier(@Valid @RequestBody SupplierDO createReqVO) {
        return success(supplierService.createSupplier(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新小程序-供应商")
    @PreAuthorize("@ss.hasPermission('qd:supplier:update')")
    public CommonResult<Boolean> updateSupplier(@Valid @RequestBody SupplierDO updateReqVO) {
        supplierService.updateSupplier(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除小程序-供应商")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qd:supplier:delete')")
    public CommonResult<Boolean> deleteSupplier(@RequestParam("id") Integer id) {
        supplierService.deleteSupplier(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得小程序-供应商")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qd:supplier:query')")
    public CommonResult getSupplier(@RequestParam("id") Integer id) {
        return success(supplierService.getSupplier(id));
    }



    @GetMapping("/page")
    @Operation(summary = "获得小程序-供应商分页")
    @PreAuthorize("@ss.hasPermission('qd:supplier:query')")
    public CommonResult getSupplierPage(@Valid SupplierPageReqVO pageVO) {
        return success(supplierService.getSupplierPage(pageVO));
    }

    public List<TreeVo> child(List<AreasDO> goodsTypes, List<TreeVo> trees){
        for (TreeVo tree : trees) {
            List<TreeVo> child=new ArrayList<>();
            for (AreasDO goodsType : goodsTypes.stream().filter(e -> e.getParentId().equals(tree.getValue())).collect(Collectors.toList())) {
                child.add(new TreeVo(goodsType.getId(),goodsType.getName()));
            }
            tree.setChildren(child);
        }
        return trees;
    }

    @GetMapping("getWeekTimes")
    public CommonResult getWeekTimes(){
        List<WeekTimes> result=new ArrayList<>();
        List<String> times=new ArrayList<>();
        times.add("00:00-01:00");
        times.add("01:00-02:00");
        times.add("02:00-03:00");
        times.add("03:00-04:00");
        times.add("04:00-05:00");
        times.add("05:00-06:00");
        times.add("06:00-07:00");
        times.add("07:00-08:00");
        times.add("08:00-09:00");
        times.add("09:00-10:00");
        times.add("10:00-11:00");
        times.add("11:00-12:00");
        times.add("12:00-13:00");
        times.add("13:00-14:00");
        times.add("14:00-15:00");
        times.add("15:00-16:00");
        times.add("16:00-17:00");
        times.add("17:00-18:00");
        times.add("18:00-19:00");
        times.add("19:00-20:00");
        times.add("20:00-21:00");
        times.add("21:00-22:00");
        times.add("22:00-23:00");
        times.add("23:00-24:00");
        result.add(new WeekTimes(1,"星期一",times));
        result.add(new WeekTimes(2,"星期二",times));
        result.add(new WeekTimes(3,"星期三",times));
        result.add(new WeekTimes(4,"星期四",times));
        result.add(new WeekTimes(5,"星期五",times));
        result.add(new WeekTimes(6,"星期六",times));
        result.add(new WeekTimes(7,"星期日",times));

        return success(result);
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出小程序-供应商 Excel")
//    @PreAuthorize("@ss.hasPermission('qd:supplier:export')")
//    @OperateLog(type = EXPORT)
//    public void exportSupplierExcel(@Valid SupplierExportReqVO exportReqVO,
//              HttpServletResponse response) throws IOException {
//        List<SupplierDO> list = supplierService.getSupplierList(exportReqVO);
//        // 导出 Excel
//        List<SupplierExcelVO> datas = SupplierConvert.INSTANCE.convertList02(list);
//        ExcelUtils.write(response, "小程序-供应商.xls", "数据", SupplierExcelVO.class, datas);
//    }

}
